package com.power.sifa.correction.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import com.power.sifa.correction.api.ICrc_person_provFeign;
import com.power.sifa.correction.model.Crc_person_prov;
import com.power.sifa.correction.model.pageInfo.Crc_person_provPageInfo;
import com.power.sifa.correction.service.Crc_person_provService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   关联省厅社矫人员控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Crc_person_provController",tags={"关联省厅社矫人员"})
public class Crc_person_provController implements ICrc_person_provFeign {

    @Resource
    private Crc_person_provService crc_person_provService;

    
    @Override
    @ApiOperation("查询关联省厅社矫人员分页列表")
    @SaCheckPermission("crc_person_prov_list")
    public ResponseDTO list(Crc_person_provPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Crc_person_prov> page = this.crc_person_provService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Crc_person_prov crc_person_prov : page) {
                crc_person_prov.geneText();
                list.add(crc_person_prov);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看关联省厅社矫人员")
    @SaCheckPermission("crc_person_prov_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Crc_person_prov crc_person_prov  = crc_person_provService.getById(id);
        if (crc_person_prov != null) {
            crc_person_prov.geneText();
        }
        return ResponseDTO.succData(crc_person_prov);
    }

    @Override
    @ApiOperation("删除关联省厅社矫人员")
    @SaCheckPermission("crc_person_prov_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        crc_person_provService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改关联省厅社矫人员")
    @SaCheckPermission("crc_person_prov_save")
    public ResponseDTO save(Crc_person_prov crc_person_prov) {
        Assert.notNull(crc_person_prov, "crc_person_prov不能为空");
        crc_person_provService.saveOrUpdate(crc_person_prov);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="批量删除关联省厅社矫人员")
    @SaCheckPermission("crc_person_prov_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        crc_person_provService.removeByIds(list);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("crc_person_prov_list")
    public void output(HttpServletResponse response, Crc_person_provPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "关联省厅社矫人员";
        String[] heads = {"crc_person的id","省厅系统的对应人员id"};
        String[] fields = {"id","ext_id"};
        Page<Crc_person_prov> page = this.crc_person_provService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }
            
    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "关联省厅社矫人员";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"crc_person的id","省厅系统的对应人员id"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }
    
	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","ext_id"};
        List<Crc_person_prov> list = (List<Crc_person_prov>)ExcelUtil.Import(fields, file, Crc_person_prov.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.crc_person_provService.saveAll(list);
        }
        return ResponseDTO.succ();
	}
    
}
